#include "StdAfx.h"
#include "ProtkCallback.h"
#include "python.h"	
#include "CallBackCode.h"



PyObject* CbToPy(CallBackObj* c)
{
	return c->callbackfu;
}

typedef void (*Py_ProUIActionT)( CallBackObj* callback,char* dialog,char *component,ProAppData appdata ); 
void Py_ProUIAction( CallBackObj* pcallback,char* dialog,char *component,ProAppData appdata ) 
{
	PyObject* callbackfu = CbToPy(pcallback);

	PyObject* pArgs(0);
	PyObject* pValue(0);
	//long re(0);
	if (callbackfu && PyCallable_Check(callbackfu)) {
		pArgs = PyTuple_New(3);
		PyObject* vi = Py_BuildValue("s", dialog);
		PyTuple_SetItem(pArgs, 0, vi);
		PyTuple_SetItem(pArgs, 1, Py_BuildValue("s", component));
		PyObject* na =	Py_None;
		Py_INCREF (na);
		PyTuple_SetItem(pArgs, 2, na);
		pValue = PyObject_CallObject(callbackfu, pArgs);
		Py_DECREF(pArgs);
		if(!pValue)
			PyErr_Print();
		else
		{
			//re = PyLong_AsLong(pValue);
			Py_DECREF(pValue);
		}
		//Py_DECREF(callbackfu);
	}
}

CallBackObj* callback = 0  ;


void pyProUIAction( char* dialog, char *component, ProAppData appdata )
{	
	Py_ProUIActionT a = Py_ProUIAction;
	int aback = 78900;
	a( (CallBackObj*)aback,dialog ,component,appdata);
}	
//&Py_ProUIAction


 CallBackObj* CallG;
void* CallBackCodeGen_33(CallBackObj* CbObj ,void * fun)
{
	 CallG = CbObj;
	 CallBackObj* a = CbObj;

	 

	 CallBackCode * pcode = new CallBackCode;
	 CallBackCode& code = *pcode;

#ifndef _WIN64
	 // 00A379C0 55               push        ebp  
	 code (push_ebp);
 // 00A379C1 8B EC            mov         ebp,esp 
	 code (mov)(esp);
 // 00A379C3 83 EC 08         sub         esp,8 
	 code (sub)(esp)(0x08);
 // Py_ProUIActionT a = Py_ProUIAction;
 // 00A379C6 C7 45 FC 70 78 A3 00 mov         dword ptr [ebp-4],0A37870h 
	 code (0xC7)(0x45)(0xFC)[(int)Py_ProUIAction];
 // CallBackObj* aback = callback;
 // 00E379BD C7 45 F8 34 34 01 00 mov         dword ptr [ebp-8],13434h 
	 code (0xC7)(0x45)(0xF8)[(int)a];
 // a( aback,dialog ,component,appdata);
 // 00A379D5 8B 45 10         mov         eax,dword ptr [ebp+10h]
	   code (mov )(0x45)(0x10);
 // 00A379D8 50               push        eax  
	   code (0x50);
 // 00A379D9 8B 45 0C         mov         eax,dword ptr [ebp+0Ch] 
	   code (mov )(0x45 )(0x0C);
 // 00A379DC 50              push        eax 
	   code (0x50);
 // 00A379DD 8B 45 08         mov         eax,dword ptr [ebp+8] 
	   code (mov )(0x45)(0x08);
 // 00A379E0 50               push        eax 
	   code (0x50);
 // 00A379E1 8B 45 F8         mov         eax,dword ptr [ebp-8] 
	   code (mov )(0x45 )(0xF8);
 // 00A379E4 50               push        eax 
	   code (0x50);
 // 00A379E5 FF 55 FC         call        dword ptr [ebp-4] 
	   code (0xFF )(0x55 )(0xFC);
 // 00A379E8 83 C4 10         add         esp,10h 
	   code (0x83 )(0xC4)(0x10);
 //
 // 00A379EB 8B E5            mov         esp,ebp 
	   code (mov)(0xE5);
 // 00A379ED 5D               pop         ebp  
	   code (pop_ebp);
 // 00A379EE C3               ret        
	   code (ret);
 // 00A379EF CC               int         3 
	   code (i_int);

   	 return code.GetFunction();

#else
	 return pyProUIAction;
#endif // _x64
 



}

void* CallBackCodeGen_3(PyObject* pyCbObj ,void * fun)
{
	CallBackObj *ac = new CallBackObj;
	 ac->callbackfu = pyCbObj;
	 return CallBackCodeGen_33(ac,fun);
}

//ProUIAction     action_cb = (ProUIAction)CallBackCodeGen_3(action ,&Py_ProUIAction);
